package com.ican.service.user;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ican.common.BusinessException;
import com.ican.common.SystemConstants;
import com.ican.common.hibernate.Page;
import com.ican.dao.user.UserDao;
import com.ican.dao.user.UserResourcesDao;
import com.ican.domain.user.User;
import com.ican.domain.user.UserResources;
import com.ican.util.ContextUtils;


@Service("userService")
public class UserService {

    private UserDao userDao;
    private UserResourcesDao userResourcesDao;
    
    public User findUserByNameAndPw(String name, String pwd){
        return userDao.findUserByNameAndPw(name, pwd);
    }
    
    public void addUser(User user){
        if(userDao.userNameIsUnique(user.getName())){
            userDao.addUser(user);
        }else{
            throw new BusinessException("用户名：" + user.getName() + "已经存在！");
        }
    }
    
    public void modifyUserNameAndPw(User user){
        if(userDao.userNameIsUnique(user.getId(), user.getName())){
            userDao.modifyUserNameAndPw(user);
        }else{
            throw new BusinessException("用户名：" + user.getName() + "已经存在！");
        }
    }
    
    public Page<User> findAllUser(Page<User> page, User user){
        return userDao.findAllUser(page, user);
    }
    
    /**
     * @param dataStatus    0:可用；1：不可用; 其他（-1）：全部
     */
    public List<User> findAllUser(int dataStatus){
        return userDao.findAllUser(dataStatus);
    }
    
    public void modifyUserStatus(Integer userId, byte dataStatus){
        userDao.modifyUserStatus(userId, dataStatus);
    }

    public User getLoginUserFromSession(){
        return (User)ContextUtils.getSessionMap().get(SystemConstants.KEY_USER_SESSION);
    }
    
    public void addUserResources(UserResources us){
        userResourcesDao.addUserResources(us);
    }
    
    public void removeUserResources(UserResources us){
        userResourcesDao.removeUserResources(us);
    }
    
    @Resource
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    @Resource
    public void setUserResourcesDao(UserResourcesDao userResourcesDao) {
        this.userResourcesDao = userResourcesDao;
    }
    
}
